package com.anuprayog.convertunits;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class NumberBaseActivity extends Activity {

	private EditText text, result;
	private Spinner spin1, spin2;
	private TextView resultString, resultString1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_number_base);
		
		text = (EditText) findViewById(R.id.editText1);
		spin1 = (Spinner) findViewById(R.id.spinner1);
		spin2 = (Spinner) findViewById(R.id.spinner2);
		result = (EditText) findViewById(R.id.editText2);
		resultString = (TextView) findViewById(R.id.textView4);
		resultString1 = (TextView) findViewById(R.id.textView5);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.number_base, menu);
		return true;
	}

	/*@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}*/

	//final Button button = (Button) findViewById(R.id.button1);
    // button.setOnClickListener(new View.OnClickListener() {
    	
	public void onClick(View view) {
			if (text.getText().length() == 0) {
				Toast.makeText(this, "Please enter a valid number",
						Toast.LENGTH_LONG).show();
				return;
			}

			long input = Long.parseLong(text.getText().toString());
			
			long input1 = input;
			String unit1 = spin1.getSelectedItem().toString();
			String unit2 = spin2.getSelectedItem().toString();
			long decimalvalue=0;
			long d=0;
			int jj=0;
			int kk=0;
			
			if (unit1.equals("Binary (Base 2)"))
			{
				while (input > 0) {
				    long ii = input % 10;
				    	
				    if (ii>1)
				    	{
				    		Toast.makeText(this, "Please enter a valid binary number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(2,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Ternary (Base 3)"))
			{
				while (input > 0) {
					long ii = input % 10;
				    	
				    if (ii>2)
				    	{
				    		Toast.makeText(this, "Please enter a valid ternary number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(3,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Quarternary (Base 4)"))
			{
				while (input > 0) {
					long ii = input % 10;
				    	
				    if (ii>3)
				    	{
				    		Toast.makeText(this, "Please enter a valid quarternary number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(4,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Quinary (Base 5)"))
			{
				while (input > 0) {
					long ii = input % 10;
				    	
				    if (ii>4)
				    	{
				    		Toast.makeText(this, "Please enter a valid quinary number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(5,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Senary (Base 6)"))
			{
				while (input > 0) {
					long ii = input % 10;
				    	
				    if (ii>5)
				    	{
				    		Toast.makeText(this, "Please enter a valid senary number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(6,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Septenary (Base 7)"))
			{
				while (input > 0) {
					long ii = input % 10;
				    	
				    if (ii>6)
				    	{
				    		Toast.makeText(this, "Please enter a valid septenary number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(7,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Octal (Base 8)"))
			{
				while (input > 0) {
					long ii = input % 10;
				    	
				    if (ii>7)
				    	{
				    		Toast.makeText(this, "Please enter a valid octal number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(8,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Nonal (Base 9)"))
			{
				while (input > 0) {
					long ii = input % 10;
				    	
				    if (ii>8)
				    	{
				    		Toast.makeText(this, "Please enter a valid nonal number",
									Toast.LENGTH_LONG).show();
							return;
				    	}
				    
				    input = input / 10;
				    decimalvalue = (long) (decimalvalue + ii*(Math.pow(9,jj)));
				    jj = jj+1;
				}
			}
			
			else if (unit1.equals("Decimal (Base 10)"))
			{
				decimalvalue = input;
			}
			
			else
			{
				decimalvalue = 0;
			}
			
			if (unit2.equals("Binary (Base 2)")){
				while (decimalvalue > 0) {
					System.out.println("*******" + decimalvalue);
					long ii = decimalvalue % 2;
					System.out.println("@@@@@@@@" + ii);
					decimalvalue = decimalvalue / 2;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				    System.out.println("========" + d);
				}
			}
			
			else if (unit2.equals("Ternary (Base 3)")){
				while (decimalvalue > 0) {
					long ii = decimalvalue % 3;
				    decimalvalue = decimalvalue / 3;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				}
			}
			
			else if (unit2.equals("Quarternary (Base 4)")){
				while (decimalvalue > 0) {
					long ii = decimalvalue % 4;
				    decimalvalue = decimalvalue / 4;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				}
			}
			
			else if (unit2.equals("Quinary (Base 5)")){
				while (decimalvalue > 0) {
					long ii = decimalvalue % 5;
				    decimalvalue = decimalvalue / 5;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				}
			}
			
			else if (unit2.equals("Senary (Base 6)")){
				while (decimalvalue > 0) {
					long ii = decimalvalue % 6;
				    decimalvalue = decimalvalue / 6;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				}
			}
			
			else if (unit2.equals("Septenary (Base 7)")){
				while (decimalvalue > 0) {
					long ii = decimalvalue % 7;
				    decimalvalue = decimalvalue / 7;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				}
			}
			
			else if (unit2.equals("Octal (Base 8)")){
				while (decimalvalue > 0) {
					long ii = decimalvalue % 8;
				    decimalvalue = decimalvalue / 8;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				}
			}
			
			else if (unit2.equals("Nonal (Base 9)")){
				while (decimalvalue > 0) {
					long ii = decimalvalue % 9;
				    decimalvalue = decimalvalue / 9;
				    d = (long) (d + ii*(Math.pow(10,kk)));
				    kk = kk+1;
				}
			}
			
			else if (unit2.equals("Decimal (Base 10)")){
				d = decimalvalue;
			}
			
			else
			{
				d = 0;
			}
			
			result.setText(String.valueOf(d));
			
			resultString.setText(String.valueOf(input1) + " " + unit1 + " =");
			resultString1.setText(String.valueOf(d) + " " + unit2);
			
		}
	
	
	public void goBack(View view) {
		finish();
	}
		
	public void goSwap(View view) {
		int spin1Index = spin1.getSelectedItemPosition();

		spin1.setSelection(spin2.getSelectedItemPosition());
		spin2.setSelection(spin1Index);
	}
}

